package com.example.f8399.testdemo.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.example.f8399.testdemo.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019-01-19.
 */

public abstract class BaseBindingAdapter<M> extends RecyclerView.Adapter<BindingViewHolder> {
    private Context mContext;
    private List<M> mList;

    public OnItemClickListener mOnItemClickListener;

    public BaseBindingAdapter(Context context) {
        mContext = context;
        mList = new ArrayList<>();
    }


    @NonNull
    @Override
    public BindingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding inflate = DataBindingUtil.inflate(LayoutInflater.from(mContext), getLayoutId(viewType), parent, false);
        return new BindingViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull BindingViewHolder holder, int position) {
        this.onBindItem(holder, position);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setData(List<M> list) {
       mList = list;
       notifyDataSetChanged();
    }

    public List<M> getData() {
        return mList;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    protected abstract @LayoutRes
    int getLayoutId(int viewType);

    protected abstract void onBindItem(@NonNull BindingViewHolder holder, int position);




}
